


//
//  NBaobiaoSearchVC.m
//  nncloud
//
//  Created by macfai on 2018/12/21.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NBaobiaoSearchVC.h"
#import "NMineCell.h"
#import "ITDatePickerController.h"
@interface NBaobiaoSearchVC ()<UITableViewDataSource,UITableViewDelegate,ITDatePickerControllerDelegate,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSArray *titleArray;

@property (nonatomic,strong)UILabel *startTimeLabel;
@property (nonatomic,strong)UILabel *endTimeLabel;

@property (nonatomic,copy)NSString *startTime;
@property (nonatomic,copy)NSString *endTime;


@property (nonatomic,copy)NSString *entName;
@property (nonatomic,copy)NSString *tableName;



@end

@implementation NBaobiaoSearchVC

#define cellIdentifier  @"mineid"
#define HeaderHight  290
#define PageSize 20
#define cellHeight 80.f
#define LabelWidth 120.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = cellHeight;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NMineCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    
    ////工业报表和财务报表共用页面
    [self.navigationItem setItemWithTitle:@"报表搜索" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    self.titleArray = @[@"企业名称",@"报表名称",@"报表月份",];
    [self addFooter];
}

- (void)addFooter {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100.f)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 30, ScreenWidth-60, 50);
    btn.centerX = footer.centerX;
    btn.backgroundColor = ThemeBlue;
    btn.layer.cornerRadius = 5.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    [btn addTarget: self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    self.tableView.tableFooterView = footer;
    
}

- (void)tapAction{
    //搜索
    //提交时候进行字符串判断，防止开始时间大于结束时间
    [self.view endEditing:YES];
    
    ///industryName工业报表    financeName财务报表
    NSDictionary *dic;
    if ([NUserModel shareInstance].isIndustryTable) {
        
         dic = @{@"startTime":self.startTime?:@"",@"endTime":self.endTime?:@"",@"enterpriseName":self.entName?:@"",@"industryName":self.tableName?:@"",@"size":@100};
    }else{
        
        dic = @{@"startTime":self.startTime?:@"",@"endTime":self.endTime?:@"",@"enterpriseName":self.entName?:@"",@"financeName":self.tableName?:@"",@"size":@100};
    }
   
   
    //通过block  把参数扔过去
    WeakStament(ws);
    [self dismissViewControllerAnimated:YES completion:^{
        if (ws.paraBlock) {
            ws.paraBlock(dic);
        }
    }];
}

#pragma mark - delegate datasource

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NMineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.nameLabel.text = self.titleArray[indexPath.row];
    cell.nameLabelLeftMargin.constant = 25.f;
    cell.setLabel.hidden = YES;
    cell.arrowImg.hidden = YES;
    cell.titleTextField.hidden = false;
    cell.titleTextField.placeholder = @"请输入";
    cell.textFieldLeftMargin.constant = 150.f;
    cell.titleTextField.tag = indexPath.row+500;
    cell.titleTextField.delegate = self;
    
    
    if (indexPath.row==2) {
        cell.titleTextField.hidden = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(startAction:)];
        UILabel *startLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, LabelWidth, cellHeight)];
        startLabel.text = @"请选择开始时间";
        self.startTimeLabel = startLabel;
//        startLabel.text = self.startTime;
        startLabel.textColor = kDarkGrayColor;
        startLabel.font = kFont14;
        startLabel.userInteractionEnabled = YES;
//        startLabel.backgroundColor = kMagentaColor;
        startLabel.textAlignment = NSTextAlignmentCenter;
        [startLabel addGestureRecognizer:tap];
        [cell.contentView addSubview:startLabel];
        
        
        UILabel *Label = [[UILabel alloc]initWithFrame:CGRectMake(220, 0, 30, cellHeight)];
        Label.text = @"——";
        Label.textColor = kDarkGrayColor;
//        Label.backgroundColor = kMagentaColor;
        Label.font = kFont14;
        Label.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:Label];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(endAction:)];
        UILabel *endLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 0, LabelWidth, cellHeight)];
        [endLabel addGestureRecognizer:tap1];
        endLabel.text = @"请选择结束时间";
//        endLabel.text = self.endTime;
        self.endTimeLabel = endLabel;
        endLabel.textColor = kDarkGrayColor;
        endLabel.userInteractionEnabled = YES;
        endLabel.font = kFont14;
//        endLabel.backgroundColor = kMagentaColor;
        endLabel.textAlignment = NSTextAlignmentCenter;
        [endLabel addGestureRecognizer:tap1];
        [cell.contentView addSubview:endLabel];
    }
    return cell;
}

///开始时间
- (void)startAction:(UITapGestureRecognizer*)tap{
   
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 600;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];
}

///结束时间
- (void)endAction:(UITapGestureRecognizer*)tap{
    
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 601;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];
    
}

#pragma mark - picker delegate

- (void)datePickerController:(ITDatePickerController *)datePickerController didSelectedDate:(NSDate *)date dateString:(NSString *)dateString {
    
//    NSInteger tag = datePickerController.tag;
//    UIButton *button = [self.view viewWithTag:tag];
//    [button setTitle:dateString forState:UIControlStateNormal];
    if (datePickerController.tag == 600) {
        YYLog(@"%@",dateString);
        self.startTime = dateString;
        self.startTimeLabel.text = dateString;
        
    } else {

        self.endTime = dateString;
        self.endTimeLabel.text = dateString;
        
    }
    
//    [self.tableView reloadData];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField.tag==500) {
        
        self.entName = textField.text;
        YYLog(@"%@",self.entName);
        
    }else{

        self.tableName = textField.text;
         YYLog(@"%@",self.tableName);
    }
}

@end
